<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" center fullscreen>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-form-item label="主类型id" prop="masterType">
        <el-select v-model="dataForm.masterType" placeholder="请选择">
          <el-option label="营养干预" value="2"></el-option>
          <el-option label="药物干预" value="1"></el-option>
          <el-option label="物理干预" value="3"></el-option>
          <el-option label="环境干预" value="0"></el-option>
          <el-option label="光学干预" value="4"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="类型名称" prop="typeName">
        <el-input v-model="dataForm.typeName" placeholder="类型名称"></el-input>
      </el-form-item>
      <el-form-item label="编码" prop="code">
        <el-input v-model="dataForm.code" placeholder="编码"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="0">停用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="序号" prop="sortNum">
        <el-input v-model="dataForm.sortNum" placeholder="序号"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {
    getPvmArcInterventionTypeObj,
    addPvmArcInterventionTypeObj,
    putPvmArcInterventionTypeObj
  } from '@/api/pvm/interventiontype'

  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          id: 0,
          masterType: '',
          typeName: '',
          code: '',
          status: 1,
          sortNum: ''
        },
        dataRule: {
          masterType: [{
            required: true,
            message: '主类型id不能为空',
            trigger: 'blur'
          }],
          typeName: [{
            required: true,
            message: '类型名称不能为空',
            trigger: 'blur'
          }],
          status: [{
            required: true,
            message: '状态(1-启用,0-停用)不能为空',
            trigger: 'blur'
          }],
          sortNum: [{
            required: true,
            message: '序号不能为空',
            trigger: 'blur'
          }],
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || ''
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getPvmArcInterventionTypeObj(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              putPvmArcInterventionTypeObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('修改成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
              })
            } else {
              addPvmArcInterventionTypeObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
              })
            }
          }
        })
      }
    }
  }
</script>
